package com.main.dao;

import java.util.Calendar;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.main.pojo.Video;
import com.main.utils.Constant;

@Transactional
@Repository
public class VideoDAOImpl implements VideoDAO {

	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Video> getVideos(int pageNumber) throws Exception {
		try {
			String hql = "FROM Video v ORDER BY v.id DESC";
			Query query = sessionFactory.getCurrentSession().createQuery(hql);
			query.setFirstResult((pageNumber - 1) * Constant.VIDEO_PAGE_SIZE);
			query.setMaxResults(Constant.VIDEO_PAGE_SIZE);
			return query.list();
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public Video getVideoById(int videoId) throws Exception {
		try{
			Video video = (Video)sessionFactory.getCurrentSession().get(Video.class, videoId);
			updateViewNumber(video.getId());
			return video;
		}catch(Exception e){
			throw e;
		}
	}
	
	private void updateViewNumber(int videoId) throws Exception{
		try{
			String hql = "UPDATE Video v SET v.viewNumber = v.viewNumber + 1 WHERE v.id = :id";
			Query query = sessionFactory.getCurrentSession().createQuery(hql);
			query.setInteger("id", videoId);
			query.executeUpdate();
		}catch(Exception e){
			throw e;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Video> getMostView() throws Exception {
		try {
			String hql = "FROM Video v WHERE v.createDate >= :createDate ORDER BY v.viewNumber DESC, v.createDate DESC";
			Query query = sessionFactory.getCurrentSession().createQuery(hql);
	        Calendar cal = Calendar.getInstance();
	        cal.add(Calendar.DATE, -3); // get most viewed in 3 recent days.    
			query.setDate("createDate", cal.getTime());
			query.setFirstResult(0);
			query.setMaxResults(Constant.MOST_VIEWED_SIZE);
			return query.list();
		} catch (Exception e) {
			throw e;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Video> getHotVideo() throws Exception {
		try {
			String hql = "FROM Video v WHERE v.createDate >= :createDate ORDER BY v.viewNumber DESC, v.createDate DESC";
			Query query = sessionFactory.getCurrentSession().createQuery(hql);
	        Calendar cal = Calendar.getInstance();
	        cal.add(Calendar.DATE, -7); // get most viewed in 1 week.    
			query.setDate("createDate", cal.getTime());
			query.setFirstResult(0);
			query.setMaxResults(Constant.HOT_VIDEO_SIZE);
			return query.list();
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public void updateComment(int id) throws Exception {
		try {
			String hql = "UPDATE Video v SET v.commentNumber = (v.commentNumber + 1) WHERE v.id = " + id;
			Query query = sessionFactory.getCurrentSession().createQuery(hql);
			query.executeUpdate();
		} catch (Exception e) {
			throw e;
		}
	}

}
